package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.EnterpriseType;
import com.csjy.migration.hnnd.dict.Industry;
import com.csjy.migration.hnnd.dict.IndustryCode;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 客户表（法人）
 */
@BusinessDataConverter(
        auto = false,

        insertSql = "insert into nd_enterprise_info (ID, CUSTOMER_ID, INDUSTRY, SET_TIME, MASTER, MASTER_PHONE, WORK_ADDRESS, OPERATER, OPERATER_PHONE, OPERATER_JOB, LEGAL_REP, LEGAL_REP_PHONE, REGIST_MONEY, CIQ_NO, WORKER_NUMBER, HANDLE_TIME, MASTER_IDCARD, LEGAL_REP_IDCARD, BUSINESS_NO, ORG_NO, TAX_REGIST_NO, CREDIT_CARD_NO, SECOND_BUSINESS, BANK_NAME, BANK_ACCOUNT_NO, CREDIT_GRADE, CREDIT_GRADE_ORG, COMPANY_IMP_MSG, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, IS_BLACKLIST, BLACKLIST_MSG, INDUSTRY_CODE, ENTERPRISE_TYPE)" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",


        updateSql = "UPDATE nd_enterprise_info SET CUSTOMER_ID = ?,INDUSTRY = ?,SET_TIME = ?,MASTER = ?,MASTER_PHONE = ?,WORK_ADDRESS = ?,OPERATER = ?,OPERATER_PHONE = ?,OPERATER_JOB = ?,LEGAL_REP = ?,LEGAL_REP_PHONE = ?,REGIST_MONEY = ?,CIQ_NO = ?,WORKER_NUMBER = ?,HANDLE_TIME = ?,MASTER_IDCARD = ?,LEGAL_REP_IDCARD = ?,BUSINESS_NO = ?,ORG_NO = ?,TAX_REGIST_NO = ?,CREDIT_CARD_NO = ?,SECOND_BUSINESS = ?,BANK_NAME = ?,BANK_ACCOUNT_NO = ?,CREDIT_GRADE = ?,CREDIT_GRADE_ORG = ?,COMPANY_IMP_MSG = ?,CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,IS_BLACKLIST = ?,BLACKLIST_MSG = ?,INDUSTRY_CODE = ?,ENTERPRISE_TYPE = ? WHERE ID = ?",


        findMigratedIdListSql = "SELECT ID FROM nd_enterprise_info"
)
public class NdEnterpriseInfo   extends AbstractBusiness{

    public NdEnterpriseInfo(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }
    @Override
    public void findMigratedIdList() throws SQLException {
        this.migratedIdList = this.targetDbUtil.selectSingleColumn(findMigratedIdListSql, new ArrayList());
    }
    public void customMigrate(Map row) throws Exception {
        int needMigratedResult = this.needMigrated(row.get("ENTERPRISENO").toString());
        if(needMigratedResult == 0) {
            return;
        }

        try{

            if(needMigratedResult == 1) {
                this.targetDbUtil.updateByParams(updateSql,this.buildValues(row),true);

//            this.batchUpdateValues.add(this.buildValues(row));
            } else {
                this.targetDbUtil.updateByParams(insertSql,this.buildValues(row),false);
//            this.batchInsertValues.add(this.buildValues(row));
            }
        }catch (Exception e) {
            throw new Exception(e);
        }

    }

    @Override
    public void migrate() throws  SQLException {

    }
    public List buildValues(Map btEnterpriseBasic) throws SQLException, ScriptException {
        List values = new ArrayList();
        //ID	主键
        values.add(btEnterpriseBasic.get("ENTERPRISENO").toString());
        //CUSTOMER_ID	客户id
        values.add(btEnterpriseBasic.get("ENTERPRISENO").toString());
        //INDUSTRY	行业归属
        values.add(DictTool.getTargetDictCode(Industry.class,btEnterpriseBasic.get("AFFTRADE")));
        //SET_TIME	成立时间
        values.add(btEnterpriseBasic.get("ESTABLISHTIME"));
        //MASTER	实际控制人
        values.add(btEnterpriseBasic.get("LINKMAN"));
        //MASTER_PHONE	联系手机
        values.add(btEnterpriseBasic.get("LINKMANTEL"));
        //WORK_ADDRESS	办公地址
        values.add(btEnterpriseBasic.get("PROADDRESS"));
        //OPERATER	经办人名
        values.add(btEnterpriseBasic.get("FAX"));
        //OPERATER_PHONE	联系电话（经办人）
        values.add(btEnterpriseBasic.get("LINKMANCELLPHONE"));
        //OPERATER_JOB	经办人职务
        values.add(btEnterpriseBasic.get("LINKMANDUTY"));
        //LEGAL_REP	法人代表
        values.add(btEnterpriseBasic.get("CORPORATION"));
        //LEGAL_REP_PHONE	法人电话
        values.add(btEnterpriseBasic.get("CORPORATIONTEL"));
        //REGIST_MONEY	注册资本（万）
        values.add(Tool.covertToBigDecimal(btEnterpriseBasic.get("REGISTCAPITAL")));
        //CIQ_NO	海关编码
        values.add(btEnterpriseBasic.get("EMAIL"));
        //WORKER_NUMBER	从业人数
        values.add(Tool.covertToBigDecimal(btEnterpriseBasic.get("STAFFCOUNT")));
        //TODO HANDLE_TIME	受理时间
        values.add(btEnterpriseBasic.get("CREATE_TIME"));
        //TODO MASTER_IDCARD	实际控制人身份证编码
        values.add(btEnterpriseBasic.get("CONROLMANID"));
        //TODO LEGAL_REP_IDCARD	法人代表身份证编码
        values.add(btEnterpriseBasic.get("CORPORATIONID"));
        //BUSINESS_NO	营业执照编号||社会信用代码
        values.add(btEnterpriseBasic.get("BUSIREGISTCODE"));
        //ORG_NO	组织机构代码证编号
        values.add(btEnterpriseBasic.get("REGORGCODE"));
        //TAX_REGIST_NO	税务登记证号
        values.add(btEnterpriseBasic.get("TAXREGISTCODE"));
        //CREDIT_CARD_NO	贷款卡号 btEnterpriseBasic.get("LOANCARDCODE")
        values.add(null);
        //SECOND_BUSINESS	兼营业务
        values.add(btEnterpriseBasic.get("DOUBLE_DEALAREA"));
        //TODO BANK_NAME	开户银行名称 btEnterpriseBasic.get("NEWTEPROCERCODE")
        values.add(null);
        //TODO BANK_ACCOUNT_NO	开户银行账号 btEnterpriseBasic.get("LOANCARDCODE")
        values.add(null);
        //TODO CREDIT_GRADE	信用评价等级 btEnterpriseBasic.get("CREDIT_ASSESS_GRADE")
        values.add(null);
        //TODO CREDIT_GRADE_ORG	信用评级机构 btEnterpriseBasic.get("NULLPATENTCODE")
        values.add(null);
        //COMPANY_IMP_MSG	企业重要情况
        values.add(Tool.decodeUnicode(btEnterpriseBasic.get("MOSTLY_DEALAREA")));
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btEnterpriseBasic.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btEnterpriseBasic.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(1);
        //IS_BLACKLIST	黑名单
        values.add(null);
        //BLACKLIST_MSG	黑名单说明
        values.add(null);
        //INDUSTRY_CODE	行业code
        values.add(DictTool.getTargetDictCode(IndustryCode.class,btEnterpriseBasic.get("AFFTRADE")));
        //ENTERPRISE_TYPE	企业类型
        values.add(DictTool.getTargetDictCode(EnterpriseType.class,btEnterpriseBasic.get("CLIENTKIND")));
        return values;
    }
}


